<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $fillable = [
        'user_id',              #评论者id
        'user_type',            #评论者的所属Model
        'object_id',            #被评论对象的id
        'object_type',          #被评论对象的所属Model
        'content',              #评论内容
        'like_amount',          #点赞量
        'is_show',
    ];

    protected $casts = [
        'is_show' => 'boolean',
    ];

    public function user()
    {
        return $this->morphTo()->withDefault();
    }

    public function object()
    {
        return $this->morphTo()->withDefault();
    }

    public function children()
    {
        return $this->morphMany(Comment::class, 'object');
    }

    public function allChildren()
    {
        return $this->children()->with('allChildren');
    }
}
